#!/usr/bin/env python

# Copyright (C) 2018 Intel Corporation

# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

from __future__ import (
    absolute_import, division, print_function, unicode_literals
)

# This script is used to generate vr-enum-table.h from vulkan.h. It
# is not run automatically as part of the build process but if need be
# it can be used to update the file as follows:
#
# ./make-enums.py < /usr/include/vulkan/vulkan.h > vr-enum-table.h

import re
import sys
from mako.template import Template

ENUMS = [
    'VkPolygonMode',
    'VkCullModeFlagBits',
    'VkFrontFace',
    'VkPrimitiveTopology',
    'VkLogicOp',
    'VkBlendFactor',
    'VkBlendOp',
    'VkColorComponentFlagBits',
    'VkCompareOp',
    'VkStencilOp'
]
VALUE_RE = re.compile(r'\s*\b(VK_(?:[A-Z0-9_]+))\b')
ENUM_START_RE = re.compile('\s*typedef\s+enum\s+(Vk[A-Za-z0-9]+)')
ENUM_END_RE = re.compile('}')
PREPROCESSOR_RE = re.compile(r' *#')

TEMPLATE="""\
/* Automatically generated by make-enums.py */
static const struct vr_enum
enums[] = {
% for e in enums:
	{ "${e}", ${e} },
% endfor
};"""


def get_enums(data):
    enums = set()
    in_enum = False

    for line in data:
        if PREPROCESSOR_RE.match(line):
            continue
        md = ENUM_START_RE.match(line)
        if md:
            in_enum = md.group(1) in ENUMS
        if ENUM_END_RE.match(line):
            in_enum = False
        if not in_enum:
            continue
        md = VALUE_RE.match(line)
        if md:
            enums.add(md.group(1))

    return sorted(enums)


def main():
    template = Template(TEMPLATE)
    print(template.render(enums = get_enums(sys.stdin)))


if __name__ == '__main__':
    main()
